package com.umsoft.chapter3.service;

import com.umsoft.chapter3.model.Customer;
import com.umsoft.smart.framework.annotation.Service;
import com.umsoft.smart.framework.helper.DatabaseHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 提供客户数据服务
 */
@Service
public class CustomerService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerService.class);

    /**
     * 获取客户列表
     * @return
     */
    public List<Customer> getCustomerList(){
        String sql = "SELECT * FROM customer";
        List<Customer> customerList = DatabaseHelper.queryEntityList(Customer.class,sql);
        return customerList;
    }


    /**
     * 获取客户
     * @param id
     * @return
     */
    public Customer getCustomer(Long id){
        String sql = "SELECT * FROM customer where id=?";
        return DatabaseHelper.queryEntity(Customer.class,sql,id);
    }

    /**
     * 创建客户
     * @param fieldMap
     * @return
     */
    public boolean createCustomer(Map<String,Object> fieldMap){
        return DatabaseHelper.insertEntity(Customer.class,fieldMap);
    }

    /**
     * 更新客户
     * @param id
     * @param fieldMap
     * @return
     */
    public Boolean updateCustomer(Long id,Map<String,Object> fieldMap){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("id",id);
        return DatabaseHelper.updateEntity(Customer.class,fieldMap,paramMap);
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    public Boolean deleteCustomer(Long id){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("id",id);
        return DatabaseHelper.deleteEntity(Customer.class,paramMap);
    }
}
